package com.slicktechnologies.client.views.settings.employee;

import java.util.Date;
import java.util.Vector;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.IntegerBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.datepicker.client.DateBox;
import com.simplesoftwares.client.library.FieldType;
import com.simplesoftwares.client.library.FormField;
import com.simplesoftwares.client.library.FormFieldBuilder;
import com.simplesoftwares.client.library.appskeleton.AppMemory;
import com.simplesoftwares.client.library.appskeleton.ScreeenState;
import com.simplesoftwares.client.library.appstructure.formscreen.FormScreen;
import com.simplesoftwares.client.library.appstructure.search.Filter;
import com.simplesoftwares.client.library.appstructure.search.MyQuerry;
import com.simplesoftwares.client.library.composite.AddressComposite;
import com.simplesoftwares.client.library.composite.SocialInfoComposites;
import com.simplesoftwares.client.library.composite.UploadComposite;
import com.simplesoftwares.client.library.mywidgets.DateBoxWithYearSelector;
import com.simplesoftwares.client.library.mywidgets.EmailTextBox;
import com.simplesoftwares.client.library.mywidgets.MyLongBox;
import com.simplesoftwares.client.library.mywidgets.ObjectListBox;
import com.simplesoftwares.client.library.mywidgets.PhoneNumberBox;
import com.slicktechnologies.client.utility.AppUtility;
import com.slicktechnologies.client.utility.Screen;
import com.slicktechnologies.client.views.composites.PassportComosite;
import com.slicktechnologies.shared.common.businessunitlayer.Branch;
import com.slicktechnologies.shared.common.helperlayer.Config;
import com.slicktechnologies.shared.common.personlayer.Employee;

/**
 * FormTablescreen template.
 */
public class EmployeeForm extends FormScreen<Employee>  implements ClickHandler {

	//Token to add the varialble declarations
	DateBox dbDateOfBirth,dbJoiningDate;
	ObjectListBox<Branch> lbBranch;
	EmailTextBox etbEmail;
	
	
	AddressComposite addressComposite;
	CheckBox cbStatus;
	
	TextBox tbFirstName,tbMiddleName,tbLastName;
	UploadComposite uploadComposite;
	
	MyLongBox pnbCellPhoneNo1;
	
	
	
	IntegerBox employeeId;

	
	

	
	
	

	

	
	//DoubleBox dbSalary;


	//Token to add the concrete presenter class name
	//protected <PresenterClassName> presenter;



	public EmployeeForm  () {
		super();
		createGui();

	}

	private void initalizeWidget()
	{
		
		dbDateOfBirth=new DateBoxWithYearSelector();

		dbJoiningDate=new DateBoxWithYearSelector();

		lbBranch=new ObjectListBox<Branch>();
		AppUtility.makeBranchListBoxLive(lbBranch);

		etbEmail=new EmailTextBox();

		

		addressComposite=new AddressComposite();

		cbStatus=new CheckBox();
		cbStatus.setValue(true); 
		

		tbFirstName=new TextBox();

		tbMiddleName=new TextBox();

		tbLastName=new TextBox();

		uploadComposite=new UploadComposite();

		

		pnbCellPhoneNo1=new MyLongBox();
		
		employeeId=new IntegerBox();
		employeeId.setEnabled(false);

		


		
		
		


	}

	/*
	 * Method template to create the formtable screen
	 */

	@Override
	public void createScreen() {

		//Token to initialize the processlevel menus.
		initalizeWidget();


		this.processlevelBarNames=new String[]{"New"};


		//////////////////////////////////Form Field Declaration/////////////////////////////////////////////////


		//////////////////////////// Form Field Initialization////////////////////////////////////////////////////

		//Token to initialize formfield
		FormFieldBuilder fbuilder;
		fbuilder = new FormFieldBuilder();
		FormField fgroupingEmployeeInformation=fbuilder.setlabel("Employee Information").widgetType(FieldType.Grouping).setMandatory(false).setColSpan(4).build();
		fbuilder = new FormFieldBuilder("* First Name",tbFirstName);
		FormField ftbFirstName= fbuilder.setMandatory(true).setMandatoryMsg("First Name is Mandatory !").setRowSpan(0).setColSpan(0).build();
		fbuilder = new FormFieldBuilder("Middle Name",tbMiddleName);
		FormField ftbMiddleName= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
		fbuilder = new FormFieldBuilder("* Last Name",tbLastName);
		FormField ftbLastName= fbuilder.setMandatory(true).setMandatoryMsg("Last Name is Mandatory !").setRowSpan(0).setColSpan(0).build();
		fbuilder = new FormFieldBuilder("* Date Of Birth",dbDateOfBirth);
		FormField fdbDateOfBirth= fbuilder.setMandatory(true).setMandatoryMsg("D.O.B is mandatory !").setRowSpan(0).setColSpan(0).build();
		fbuilder = new FormFieldBuilder("* Branch",lbBranch);
		FormField flbBranch= fbuilder.setMandatory(true).setMandatoryMsg("Branch is Mandatory !").setRowSpan(0).setColSpan(0).build();
		
		fbuilder = new FormFieldBuilder("Joining Date",dbJoiningDate);
		FormField fdbJoiningDate= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(0).build();

		

		fbuilder = new FormFieldBuilder("Status",cbStatus);
		FormField fcbStatus= fbuilder.setRowSpan(0).setColSpan(0).build();
		fbuilder = new FormFieldBuilder();
		FormField fgroupingEmployeeContactInformation=fbuilder.setlabel("Employee Contact Information").widgetType(FieldType.Grouping).setMandatory(false).setColSpan(4).build();
		
		fbuilder = new FormFieldBuilder("* Cell Phone No 1",pnbCellPhoneNo1);
		FormField fpnbCellPhoneNo1= fbuilder.setMandatory(true).setMandatoryMsg("Cell Phone No 1 is Mandatory !").setRowSpan(0).setColSpan(0).build();
		
		fbuilder = new FormFieldBuilder("Email",etbEmail);
		FormField fetbEmail= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
		fbuilder = new FormFieldBuilder("",addressComposite);
		FormField faddressComposite= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(4).build();
		fbuilder = new FormFieldBuilder();
		
		
		fbuilder = new FormFieldBuilder();
		FormField fgroupingUploadPhoto=fbuilder.setlabel("Upload Photo").widgetType(FieldType.Grouping).setMandatory(false).setColSpan(4).build();
		fbuilder = new FormFieldBuilder("",uploadComposite);
		FormField fuploadComposite= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
		
		
		
		
		
		
		
		
		
		
		fbuilder = new FormFieldBuilder("Employment ID",employeeId);
		FormField folbemployeeId= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
		

		fbuilder = new FormFieldBuilder();
		

		

		
		
		
		
		
		
		

		/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

		////////////////////////////////Lay Out Making Code///////////////////////////////////////////////////////////////


		FormField[][] formfield = {   {fgroupingEmployeeInformation},
				{folbemployeeId},
				{ftbFirstName,ftbMiddleName,ftbLastName,fdbDateOfBirth},
				{flbBranch,fdbJoiningDate},
				
				{fcbStatus},
				{fgroupingEmployeeContactInformation},
				{fpnbCellPhoneNo1,fetbEmail},
				{faddressComposite},
				
				{fgroupingUploadPhoto},
				{fuploadComposite},
		};


		this.fields=formfield;		
	}

	/**
	 * method template to update the model with token entity name
	 */
	@Override
	public void updateModel(Employee model) 
	{

		if(tbFirstName.getValue()!=null)
			model.setFirstName(tbFirstName.getValue());
		if(tbMiddleName.getValue()!=null)
			model.setMiddleName(tbMiddleName.getValue());
		if(tbLastName.getValue()!=null)
			model.setLastName(tbLastName.getValue());
		if(dbDateOfBirth.getValue()!=null)
			model.setDob(dbDateOfBirth.getValue());
		if(lbBranch.getValue()!=null)
			model.setBranchName(lbBranch.getValue());
		
		
		
		if(dbJoiningDate.getValue()!=null)
			model.setJoinedAt(dbJoiningDate.getValue());
		
		
		
		if(cbStatus.getValue()!=null)
			model.setStatus(cbStatus.getValue());
		
		if(pnbCellPhoneNo1.getValue()!=null)
			model.setCellNumber1(pnbCellPhoneNo1.getValue());
		
		if(etbEmail.getValue()!=null)
			model.setEmail(etbEmail.getValue());
		if(addressComposite.getValue()!=null)
			model.setAddress(addressComposite.getValue());
		
			model.setPhoto(uploadComposite.getValue());
		


		presenter.setModel(model);


	}

	/**
	 * method template to update the view with token entity name
	 */
	@Override
	public void updateView(Employee view) 
	{

        
		
		employeeId.setValue(view.getCount());
		if(view.getFirstName()!=null)
			tbFirstName.setValue(view.getFirstName());
		if(view.getMiddleName()!=null)
			tbMiddleName.setValue(view.getMiddleName());
		if(view.getLastName()!=null)
			tbLastName.setValue(view.getLastName());
		if(view.getDob()!=null)
			dbDateOfBirth.setValue(view.getDob());
		if(view.getBranchName()!=null)
			lbBranch.setValue(view.getBranchName());
		
		if(view.getEmail()!=null)
			etbEmail.setValue(view.getEmail());
		if(view.getAddress()!=null)
			addressComposite.setValue(view.getAddress());
		if(view.getPhoto()!=null)
			uploadComposite.setValue(view.getPhoto());
		
		
		
		

		
		presenter.setModel(view);

	}

	/**
	 * Toggles the app header bar menus as per screen state
	 */

	public void toggleAppHeaderBarMenu() {
		if(AppMemory.getAppMemory().currentState==ScreeenState.NEW)
		{
			InlineLabel[] menus=AppMemory.getAppMemory().skeleton.getMenuLabels();
			for(int k=0;k<menus.length;k++)
			{
				String text=menus[k].getText();
				if(text.equals("Save")||text.equals("Discard")||text.contains("Search"))
				{
					menus[k].setVisible(true); 
				}
				else
					menus[k].setVisible(false);  		   

			}
		}

		else if(AppMemory.getAppMemory().currentState==ScreeenState.EDIT)
		{
			InlineLabel[] menus=AppMemory.getAppMemory().skeleton.getMenuLabels();
			for(int k=0;k<menus.length;k++)
			{
				String text=menus[k].getText();
				if(text.equals("Save")||text.equals("Discard"))
					menus[k].setVisible(true); 

				else
					menus[k].setVisible(false);  		   
			}
		}

		else if(AppMemory.getAppMemory().currentState==ScreeenState.VIEW)
		{
			InlineLabel[] menus=AppMemory.getAppMemory().skeleton.getMenuLabels();
			for(int k=0;k<menus.length;k++)
			{
				String text=menus[k].getText();
				if(text.equals("Discard")||text.equals("Edit")||text.equals("Search"))
					menus[k].setVisible(true); 

				else
					menus[k].setVisible(false);  		   
			}
		}
	
	}

	/**
	 * sets the id textbox with the passed count value. 
	 */
	@Override
	public void setCount(int count)
	{
      this.employeeId.setValue(count);
	}


	@Override
	public void onClick(ClickEvent event) {
		

	}

	@Override
	public void clear() {

		super.clear();
		cbStatus.setValue(true); 
		
	}

	@Override
	public boolean validate() {
		// TODO Auto-generated method stub
		boolean superRes= super.validate();
		
		if(superRes==false)
		{
			return false;
		}
		
		
		
		
		
		
		else if(this.dbDateOfBirth.getValue()!=null)
		{
			int date=calculateAge(dbDateOfBirth.getValue());
			if(date>=18)
				return true;
			else
			{
				showDialogMessage("Age is less than 18!");
				return false;
			}
		}
		
		
		
		
		
		
		return true;

	}
	
	
	
	

	public static int calculateAge(Date dob)
	{
		Date today = new Date();
		Integer currentYear = new Integer(DateTimeFormat.getFormat("yyyy").format(today));
		Integer currentMonth = new Integer(DateTimeFormat.getFormat("M").format(today));
		Integer currentDay = new Integer(DateTimeFormat.getFormat("d").format(today));

		Integer dobYear = new Integer(DateTimeFormat.getFormat("yyyy").format(dob));
		Integer dobMonth = new Integer(DateTimeFormat.getFormat("M").format(dob));
		Integer dobDay = new Integer(DateTimeFormat.getFormat("d").format(dob));

		int age = currentYear - dobYear;

		if((dobMonth > currentMonth) || (currentMonth == dobMonth && dobDay > currentDay))
			age--;

		return age;
	}

	
	
	

	
	
	
	
	
	@Override
	public void setEnable(boolean state)
	{
		super.setEnable(state);
		employeeId.setEnabled(state);
		
	}
	
	
	
}
